/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wave;

import ast.*;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Vector;
import base.*;
import trackmsg.EchoMsg;
import trackmsg.TrackCtrlMsg;
/**
 *
 * @author Quest
 */

public class ASTNotContainsExecution extends WaveExecution{
    public ASTNotContainsExecution(WaveHeadParser parser, WaveEnvironment waveEnv){
        super(parser, waveEnv);
    }

    public void execute(ASTNotContains notContain, String tail){
        Vector vLeft = getLeftChild(notContain);
        Vector vRight = getRightChild(notContain);
        boolean result = false;
        if (vLeft.size() > 0 && vRight.size() > 0) {
            result = !WaveActs.contain(vLeft, vRight);
        }
        if(waveEnv.getTrackAddress() != null){
            TrackNode fromTrack = wi.getTrackForest().getTrackNode(waveEnv.getTrackAddress());
            TrackNode.Echo echo = result?echo = TrackNode.Echo.TRUE:TrackNode.Echo.FALSE;
            if(tail.equals("")){
                TrackCtrlMsg msg = new EchoMsg(fromTrack, echo, waveEnv);
                wi.getTrackQueue().incoming(msg);
            } else if(result == false){
                TrackCtrlMsg msg = new EchoMsg(fromTrack, echo, waveEnv);
                wi.getTrackQueue().incoming(msg);
            }
        }

        if(result && !tail.trim().equals("")){
            Wave newWave = new Wave(tail, waveEnv);
            waveQueue.incomingFront(newWave);
        }
        waveEnv.dump();
        //Utilities.WPPrint("Result: " + result);
        Utilities.WPPrint("Not Contains has done.");
    }

    private Vector getChild(ASTNotContains notContain, int intPos)
    {
        SimpleNode child = (SimpleNode) notContain.jjtGetChild(intPos);
        Vector v = new Vector();
        Object obj = new Object();
        if (child instanceof ASTFrontalVar){
            obj = waveEnv.getFrontalTable().get(child.getString());
            //Utilities.WPPrint("OBJ VALUE: " + obj);
        } else if (child instanceof ASTNodalVar){

            Hashtable<String, Vector> nodalTable =
                    kn.getKNNode(waveEnv.getCONTENT()).getNodalTable(waveEnv.getColor());
            obj = nodalTable.get(child.getString());
            //Utilities.WPPrint("OBJ VALUE: " + obj);
            //kn.getKNNodeByName(knNode.getName());
            //KNNode knNode = new KNNode
            //kn.getKNNode(knNode.getName());
            //KNNode knNode = waveEnv.getCONTENT();
            //obj = knNode.getNodalTable().get(child.toString());
        } else {
            obj = child.getString();
        }
        if (obj instanceof Collection){
            v.addAll((Collection)obj);
        } else {
            v.add(obj);
        }
        return  v;
    }

    public Vector getLeftChild(ASTNotContains notContain){
        return getChild(notContain, 0);
    }

    public Vector getRightChild(ASTNotContains notContain){
        return getChild(notContain, 1);
    }
}
